package com.study.system.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 超时订单队列配置
 */
@Configuration
public class TimeoutOrderMQConfig {

    //正常交换机
    public static final String ORDER_EXCHANGE = "order-exchange";
    //正常队列
    public static final String ORDER_QUEUE = "order-queue";
    //死信交换机
    public static final String DEADLETTER_ORDER_EXCHANGE = "deadletter-order-exchange";
    //死信队列
    public static final String DEADLETTER_ORDER_QUEUE = "deadletter-order-queue";
    //正常路由键
    public static final String ORDER_QUEUE_KEY = "order-queue-routekey";
    //死信路由键
    public static final String DEADLETTER_ORDER_QUEUE_KEY = "deadletter-order-queue-routekey";

    //定义正常交换机
    @Bean
    public TopicExchange orderExchange(){
        return new TopicExchange(ORDER_EXCHANGE);
    }

    //定义正常队列
    @Bean
    public Queue orderQueue(){
        Map<String,Object> args = new HashMap<>();
        //给正常队列设置死信交互机和死信路由键
        args.put("x-dead-letter-exchange",DEADLETTER_ORDER_EXCHANGE);
        args.put("x-dead-letter-routing-key",DEADLETTER_ORDER_QUEUE_KEY);
        return QueueBuilder.durable(ORDER_QUEUE).withArguments(args).build();
    }

    //绑定正常交换机和正常队列
    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_QUEUE_KEY);
    }

    //死信交换机
    @Bean
    public TopicExchange deadLetterExchange(){
        return new TopicExchange(DEADLETTER_ORDER_EXCHANGE);
    }

    //死信队列
    @Bean
    public Queue deadLetterQueue(){
        return new Queue(DEADLETTER_ORDER_QUEUE);
    }

    //绑定死信队列和死信交换机
    @Bean
    public Binding deadLetterBinding(){
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with(DEADLETTER_ORDER_QUEUE_KEY);
    }
}
